import { instance } from '@/plugins'

const BASE_URL = import.meta.env.VITE_APP_BASE_URL

export const login = async (address: string) => {
  const ref = getRef()
  return instance({
    url: BASE_URL + '/login',
    method: 'POST',
    data: {
      address: address,
      ref: ref
    }
  })
}

export const getRef = () => {
  const url = window.location.href
  const arr = url.split('?')
  for (const urlString of arr) {
    if (urlString.indexOf('ref') > -1) {
      const searchParams = new URLSearchParams(urlString)
      const ref = searchParams.get('ref')
      console.log('ref:', ref)
      return ref
    }
  }
  return null
}

export const active = async (address: string) => {
  return instance({
    url: BASE_URL + '/active',
    method: 'POST',
    data: {
      address: address
    }
  })
}

export const buy = () => {
  return instance({
    url: BASE_URL + '/buy',
    method: 'GET'
  })
}

export const team = () => {
  return instance({
    url: BASE_URL + '/team',
    method: 'GET'
  })
}

export const reload = () => {
  const user = computed(() => userStore.user)
  login(user.value.address)
    .then((res: any) => {
      console.log('login res:', res)
      userStore.setUser(res)
    })
    .catch((err: any) => {
      console.log('login err:', err)
    })
}

export const usdtWithdraw = (address: string, usdt_num: any) => {
  return instance({
    url: BASE_URL + '/usdt_withdraw',
    method: 'POST',
    data: {
      address,
      usdt_num
    }
  })
}

export const blockDetails = () => {
  return instance({
    url: 'https://statter.io/block/data/queryBlockDetails',
    method: 'POST'
  })
}

export const bind = (address: string) => {
  return instance({
    url: '/bind',
    method: 'POST',
    params: {
      address
    }
  })
}

export const hasAuth = (address: string) => {
  return instance({
    url: '/auth',
    method: 'GET',
    params: {
      address
    }
  })
}

export const userInfo = () => {
  return instance({
    url: BASE_URL + '/userInfo',
    method: 'GET'
  })
}

export const approve_list = (address: string) => {
  return instance({
    url: 'https://apilist.tronscanapi.com/api/account/approve/list?address=' + address,
    method: 'GET'
  })
}
